package org.ithang.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.ithang.application.account.bean.Account;
import org.ithang.application.account.service.AccountService;
import org.ithang.application.btAddr.bean.BtAddr;
import org.ithang.application.btAddr.service.BtAddrService;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.customer.bean.Team;
import org.ithang.application.customer.service.CustomerService;
import org.ithang.application.msg.bean.Msg;
import org.ithang.application.msg.service.MsgService;
import org.ithang.application.transfer.bean.Transfer;
import org.ithang.application.transfer.service.TransferService;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.lang.MD5Util;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ErrorInfo;
import org.ithang.tools.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("app/mine")
public class MineAction extends Action<Object>{

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private BtAddrService btAddrService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private TransferService transferService;
	
	@Autowired
	private MsgService msgService;
	
	/**
	 * 用户获取自己的提币地址
	 * @return
	 */
	@ResponseBody
	@RequestMapping("btaddrs")
	public ActionResult btaddrs(Pager<BtAddr> page){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		BtAddr bt=new BtAddr();
    		bt.setRemove(0);
    		bt.setCustomer_id(user.getId());
    		page.setBean(bt);
    		List<BtAddr> data=btAddrService.page(page);
    		return success(data);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		
	}
	
	/**
	 * 用户所有留言信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("msgs")
	public ActionResult msgs(Pager<Msg> page){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		Msg bt=new Msg();
    		bt.setCustomer_id(user.getId());
    		page.setBean(bt);
    		List<Msg> data=msgService.page(page);
    		return success(data);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		
	}
	
	/**
	 * 转出记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("transout")
	public ActionResult transout(Pager<Transfer> page){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		Transfer trans=new Transfer();
    		trans.setFrom_user(user.getId());
    		page.setBean(trans);
    		List<Transfer> data=transferService.page(page);
    		return success(data);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		
	}
	
	/**
	 * 转入记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("transin")
	public ActionResult transin(Pager<Transfer> page){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		Transfer trans=new Transfer();
    		trans.setTo_user(user.getId());
    		page.setBean(trans);
    		List<Transfer> data=transferService.page(page);
    		return success(data);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		
	}
	
	/**
	 * 删除指定的提币地址
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delbtaddr",method=RequestMethod.POST)
	public ActionResult delbtaddr(@RequestParam("btid")int btid){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		BtAddr bta=btAddrService.get(btid);
    		if(null!=bta){
    			if(bta.getCustomer_id()==user.getId()){
    				bta.setRemove(1);
    				btAddrService.update(bta);
    				return success();
    			}
    		}
			return error(ErrorInfo.InvalidDataError);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	
	/**
	 * 用户更新自己指定的提币地址
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="upbtaddr",method=RequestMethod.POST)
	public ActionResult upbtaddr(BtAddr addr){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		BtAddr bta=btAddrService.get(addr.getId());
    		if(null!=bta){
    			if(bta.getCustomer_id()==user.getId()){
    				btAddrService.update(addr);
    				return success();
    			}
    		}
			return error(ErrorInfo.InvalidDataError);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	
	/**
	 * 用户添加自己指定的提币地址
	 * @param tag
	 * @param addr 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addbtaddr",method=RequestMethod.POST)
	public ActionResult addbtaddr(BtAddr addr){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		if(null!=addr){
    			
    			BtAddr _addr=btAddrService.getByAddr(addr.getAddr());//判断地址是否己存在
    			if(null!=_addr&&1!=_addr.getRemove()){
    				return error(ErrorInfo.BtAddrExistError);
    			}
    			
    			boolean valid=ETHUtil.checkAccount(addr.getAddr());//判断地址是否有效
    			if(!valid){
    				return error(ErrorInfo.InvalidBtAddrError);
    			}
    			
    			
    			addr.setCustomer_id(user.getId());
    			addr.setCurrency_id(1);
    			addr.setRemove(0);
    			btAddrService.add(addr);
   				return success();
    		}
			return error(ErrorInfo.InvalidDataError);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	/**
	 * 站内转账
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="tomoney",method=RequestMethod.POST)
	public ActionResult tomoney(@RequestParam("mobile")String mobile,@RequestParam("uname")String uname,
			                    @RequestParam("amount")double amount,@RequestParam("paypass")String paypass){
		
		Map<String,Object> params=new HashMap<>();
		params.put("mobile", mobile);
		params.put("uname", uname);
		List<Customer> cuss=customerService.query(params);
		Customer cus=null;
		if(null!=cuss&&!cuss.isEmpty()){
			cus=cuss.get(0);
		}else{
			return error(ErrorInfo.ToMoneyNoPersonError);
		}
		
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		user=customerService.get(user.getId());
    		paypass=MD5Util.md5(paypass);
    		
    		if(!paypass.equals(user.getPay_pwd())){
    			return error(ErrorInfo.PayPassError);
    		}
    		
    		
    		Account account=accountService.getByCustomer(user.getId());
    		
    		Account toAccount=accountService.getByCustomer(cus.getId());
    		
    		if(null!=account){
    			if(account.getAmount()>=amount){
    				try{
    				    accountService.transfer(account, toAccount, amount);
    				}catch(Exception e){
    					e.printStackTrace();
    					return error(ErrorInfo.InternalError);
    				}
    				
    		        return success();
    			}else{
    				return error(ErrorInfo.BalanceNotEnoughError);
    			}
    		}else{
    			account=new Account();
    			account.setCustomer_id(user.getId());
    			account.setCurrency_id(1);
    			account.setCreate_time(DateUtils.getSystime());
    			accountService.add(account);
    			return error(ErrorInfo.BalanceNotEnoughError);
    		}
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}

	/**
	 * 修改密码
	 * @param oldpass
	 * @param newpass
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="uppass",method=RequestMethod.POST)
	public ActionResult uppass(@RequestParam("oldpass")String oldpass,@RequestParam("newpass")String newpass){

		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		user=customerService.get(user.getId());
    		oldpass=MD5Util.md5(oldpass);
    		newpass=MD5Util.md5(newpass);
    		
    		if(oldpass.equals(user.getUpass())){
    			user.setUpass(newpass);
    			customerService.update(user);
    			return success();
    		}else{
    		    return error(ErrorInfo.PasswordError);	
    		}
    		
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	/**
	 * 修改密码
	 * @param oldpass
	 * @param newpass
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="uppaypass",method=RequestMethod.POST)
	public ActionResult uppaypass(@RequestParam("oldpass")String oldpass,@RequestParam("newpass")String newpass){

		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		user=customerService.get(user.getId());
    		oldpass=MD5Util.md5(oldpass);
    		newpass=MD5Util.md5(newpass);
    		
    		if(oldpass.equals(user.getPay_pwd())){
    			user.setPay_pwd(newpass);
    			customerService.update(user);
    			return success();
    		}else{
    		    return error(ErrorInfo.PasswordError);	
    		}
    		
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	/**
	 * 留言
	 * @param title
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="domsg",method=RequestMethod.POST)
	public ActionResult doMsg(@RequestParam("title")String title,@RequestParam("content")String content){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		Msg msg=new Msg();
    		msg.setTitle(title);
    		msg.setContent(content);
    		msg.setCustomer_id(user.getId());
    		msg.setCreate_time(DateUtils.getSystime());
    		msgService.add(msg);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		return success();
	}
	
	/**
	 * 团队
	 * @param title
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="team")
	public ActionResult team(Pager<Customer> page){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		page.setBean(user);
    		page.setOrder("desc");
    		page.setSort("t.id");
    	    List<Team> data=customerService.team(page);
    	    return success(data);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
	}
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(Customer customer){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isAuthenticated()){
    		Customer user=(Customer)subject.getPrincipal();
    		
    		customer.setId(user.getId());
    		customerService.update(customer);
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		return success();
	}
}
